package com.legal.app.controller;



import com.legal.app.domain.WeChatLoginRequest;
import com.legal.common.core.domain.AjaxResult;
import com.legal.app.service.WechatAuthService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.legal.framework.datasource.DynamicDataSourceContextHolder.log;

/**
 * 微信登录控制器
 * @author Ale
 */
@RestController
@Api(tags = "微信登录")
@RequestMapping("/app/wechat")
public class WechatLoginController {
    @Autowired
    private WechatAuthService wechatAuthService;

    /**
     * 获取微信授权URL
     */
    @GetMapping("/authUrl")
    public AjaxResult getAuthUrl(@RequestParam(required = false) String state) {
        String authUrl = wechatAuthService.getAuthorizationUrl(state);
        return AjaxResult.success(authUrl);
    }

    /**
     * 微信登录回调
     */
    @PostMapping("/login")
    public Object login(@RequestBody WeChatLoginRequest request) {
        log.info("接收到微信登录请求，参数: {}", request);
        try {
            return AjaxResult.success(wechatAuthService.wechatLogin(
                    request.getCode(),
                    request.getEncryptedData(),
                    request.getIv()
            ));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("微信登录失败: " + e.getMessage());
        }
    }


}
